package Common;

import Pages.*;
import org.openqa.selenium.*;


import java.util.List;
import java.util.Random;

/**
 * main function of tests
 */
public class Application extends HelperBase{

    public Application(TestManager manager) {
        super(manager);
    }

    protected static LandingPage landing = new LandingPage();
    protected static ForwardThisPage forward = new ForwardThisPage();
    protected static ArticlePage articlePage = new ArticlePage();
    protected static LoginPage login = new LoginPage();
    protected static MenuPages menu = new MenuPages();


    public void yourEmailValidator(String validator){
        type(By.xpath(forward.email()), validator);
    }
    public void sendToValidator(String validator){
        type(By.xpath(forward.recipients()), validator);
    }
    public void yourEmailClear(){
        ClearField(By.xpath(forward.email()));
    }
    public void sendToClear(){
        ClearField(By.xpath(forward.recipients()));
    }


    // ForwardThisPage test
    public void alertWait() {
        waitForElementVisible(By.xpath(forward.invalidAlert()));
    }
    public void messageSending(String mail){
        type(By.xpath(forward.email()), mail);
        type(By.xpath(forward.name()), "UserName");
        type(By.xpath(forward.recipients()), "legolasss_07@mail.ru");
        type(By.xpath(forward.message()), "its first test");
        click(By.xpath(forward.submit()));
    }
    public void goToArticle3(){
        click(By.xpath(landing.articlePage3()));
    }
    public void goToForwardThisPage() {
        click(By.xpath(articlePage.forwardThisPage()));
    }

    // NavigationMenu test
    public void goToTechnologiesPage() {
        click(By.xpath(landing.technologies()));
    }
    public void goToNewsPage() {
        click(By.xpath(landing.news()));
    }
    public void goToMarketsPage() {
        click(By.xpath(landing.markets()));
    }
    public void goToCompaniesPage() {
        click(By.xpath(landing.companies()));
    }
    public void goToPartsPage() {
        click(By.xpath(landing.parts()));
    }
    public void goToAdsPage() {
        click(By.xpath(landing.ads()));
    }
    public void goToCommunityPage() {
        click(By.xpath(landing.community()));
    }
    public void goToStorePage() {
        click(By.xpath(landing.store()));
    }
    public void nextPage() {
        click(By.xpath(menu.nextButton()));
    }
    public void previousPage() {
        click(By.xpath(menu.previousButton()));
    }

    //Article page test
    public void goToArticle4(){
        click(By.xpath(landing.articlePage4()));
    }
    public List<WebElement> commenentTextFields(){
        return findElements(By.xpath(articlePage.commentTextField()));
    }
    public List<WebElement> commenentSubmitButtons(){
        return findElements(By.xpath(articlePage.commentSubmitButton()));
    }
    public List<WebElement> commenentResetButtons(){
        return findElements(By.xpath(articlePage.commentResetButton()));
    }
    public void commentEdit(String data) {
        commenentTextFields().get(commenentTextFields().size() - 1).sendKeys(data);
        commenentSubmitButtons().get(commenentSubmitButtons().size() - 1).click();
    }
    public void resetButton(List<WebElement> a, List<WebElement> b, int i) {
        a.get(i).sendKeys("piu" + i + " " + i);
        sleeper(500);
        b.get(i).click();
    }
    public int randomizer(){
        Random random = new Random();
        return random.nextInt(1000);
    }
    public void votingPlus(){
        click(By.xpath(articlePage.plusButton()));
    }
    public void votingMinus(){
        click(By.xpath(articlePage.minusButton()));
    }
    public boolean votingAssert() {
        return (isElementPresent(By.xpath(articlePage.minusButtonDisabled()))
                & (isElementPresent(By.xpath(articlePage.plusButtonDisabled()))));
    }

    // Size changing
    public void reductionCommentsTextAreaSize() {
        int y = getElementHeight(commenentTextFields().get(commenentTextFields().size() - 1));
        int x = getElementWidth(commenentTextFields().get(commenentTextFields().size() - 1));
        mouseMoving(commenentTextFields().get(commenentTextFields().size() - 1), x/2-9, y/2-2, -x, -y);
    }
    public void reductionMessageTextAreaSize(){
        int x = getElementWidth(findElement(By.xpath(forward.message())));
        int y = getElementHeight(findElement(By.xpath(forward.message())));
        mouseMoving(findElement(By.xpath(forward.message())), x/2-4, y/2-5, -x, -y);
    }
    public boolean isSizeReduced(WebElement element){
        return element.getSize().getWidth() == 26 & element.getSize().getHeight() == 17;
    }
    public int increaseCommentsTextAreaSize() {
        int y = getElementHeight(commenentTextFields().get(commenentTextFields().size() - 1));
        int x = getElementWidth(commenentTextFields().get(commenentTextFields().size() - 1));
        mouseMoving(commenentTextFields().get(commenentTextFields().size() - 1), x/2-9, y/2-2, 100, 0);
        System.out.println(x);
        return x;
    }

    //Authorization
    public void goToLogInPage(){
        moveTheMouse(findElement(By.xpath(landing.user())));
        click(By.xpath(landing.logIn()));
    }
    public void authorization() {
        userLogin(By.xpath(login.userName()), By.xpath(login.userPass()), By.xpath(login.loginButton()),
                manager.getMainLogin(), manager.getMainPassword());
    }
    public void authorizationWithWrongPass() {
        userLogin(By.xpath(login.userName()), By.xpath(login.userPass()), By.xpath(login.loginButton()),
                manager.getMainLogin(),"invPass");
    }
    public boolean isLogin() {
        return true; //findElement(By.xpath("")).equals("");
    }

}
